package com.yango.serviceutil.handler;

import com.yango.commonutil.result.Result;
import com.yango.serviceutil.exception.YangoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Li.Ding.yin
 * @create 2021年07月22日10:32-星期四
 * @role 统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.error();
    }

    //特定异常处理-分母不能为零
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.error().message("执行了自定义异常!");
    }

    //自定义异常处理
    @ExceptionHandler(YangoException.class)
    @ResponseBody
    public Result error(YangoException e) {
        e.printStackTrace();
        return Result.error().message(e.getMessage()).code(e.getCode());
    }

}
